import sun.reflect.generics.tree.Tree;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

public class TestBinaryTree {
    static class TreeNode{
        public char val;//数据域
        public TreeNode left;//左孩子引用
        public TreeNode right;//右孩子引用

        public TreeNode(char val) {
            this.val = val;
        }
    }
    public TreeNode root;//根节点
    public TreeNode createTree(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        //this.root = A;
        return A;
    }
    //三种遍历方法
    public void preOrder(TreeNode root){//前序遍历
        if (root == null){
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }
    //非递归前序遍历
    public void preOrderNor(TreeNode root){
        //模拟遍历的终止条件
        if (root == null){
            return;
        }
        TreeNode cur = root;
        Deque<TreeNode> stack = new ArrayDeque<>();
        //如果指针cur不为空且栈中还有元素，说明遍历未结束
        while (cur != null || !stack.isEmpty()){
            //如果cur不为空，将其入栈（用以和其右子树产生联系）并打印，然后查看其左子树，
            //直到左子树为空
            while (cur != null){
                stack.push(cur);
                System.out.println(cur.val + " ");
                cur = cur.left;
            }
            //此时左子树为空，弹出栈顶元素开始遍历其右子树
            TreeNode top = stack.pop();
            cur = top.right;
        }
    }
    //非递归中序遍历
    public void inOrderNor(TreeNode root){
        if (root == null){
            return;
        }
        TreeNode cur = root;
        Deque<TreeNode> stack = new ArrayDeque<>();
        while (cur != null || !stack.isEmpty()){
            while (cur != null){
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            System.out.println(cur.val + " ");
            cur = top.right;
        }
    }
    public void inOrder(TreeNode root){//中序遍历
        if (root == null){
            return;
        }
        preOrder(root.left);
        System.out.print(root.val + " ");
        preOrder(root.right);
    }
    //非递归后序遍历
    public void postOrderNor(TreeNode root){
        if (root == null){
            return;
        }
        TreeNode cur = root;
        TreeNode prev = null;
        Deque<TreeNode> stack = new ArrayDeque<>();
        while (cur != null || !stack.isEmpty()){
            while (cur != null){
                stack.push(cur);
                cur = cur.left;
            }
            //此时左子树已经遍历完了，但是还不能弹出栈顶元素。
            //因为这是后续遍历，根节点要在右结点打印后才能打印
            //现在弹出去后面就没法打印这个根节点了
            TreeNode top = stack.peek();
            if (top.right == null || top.right == prev){
                System.out.println(top.val + " ");
                stack.pop();
                prev = top;
            }else {
                cur = top.right;
            }
        }
        System.out.println();
    }
    public void postOrder(TreeNode root){//后序遍历
        preOrder(root.left);
        preOrder(root.right);
        System.out.print(root.val + " ");
    }
    public List<Character> preorderTraversal(TreeNode root){//使用了返回值的前序遍历
      List<Character> ret = new ArrayList<>();
      if (root == null){
          return ret;
      }
      ret.add(root.val);
      List<Character> leftTree = preorderTraversal(root.left);
      ret.addAll(leftTree);
      List<Character> rightTree = preorderTraversal(root.right);
      ret.addAll(rightTree);

      return ret;
    }

    //获取树中节点的个数——左树节点+右树结点+1
    public int size(TreeNode root){
        if (root == null){
            return 0;
        }
        int leftSize = size(root.left);
        int rightSize = size(root.right);
        return leftSize+rightSize+1;
    }
    //遍历思路：遇到节点就加1
    public int nodeSize;
    public void size2(TreeNode root){
        if (root == null){
            return;
        }
        nodeSize++;
        size2(root.left);
        size2(root.right);
    }
    //获取叶子节点的个数
    int getLeafNodeCount(TreeNode root){
        if (root == null){
            return 0;
        }
        if (root.left == null && root.right == null){
            return 1;
        }
        int leftSize = getLeafNodeCount(root.left);
        int rightSize = getLeafNodeCount(root.right);
        return leftSize + rightSize;
    }
    //获取第K层节点的个数
    int getKleveNodeCount(TreeNode node,int k){
        if (root == null){
            return 0;
        }
        if (k == 1){
            return 1;
        }
        int leftSize = getKleveNodeCount(root.left,k-1);
        int rightSize = getKleveNodeCount(root.right,k-1);
        return leftSize + rightSize;
    }
    //获取二叉树的高度——节点为A的这个树的高度=左树高度和右树高度的最大值+1
    public int getHeight(TreeNode root){
        if (root == null){
            return 0;
        }
        int leftHight = getHeight(root.left);
        int rightHight = getHeight(root.right);
        return ((leftHight>rightHight) ? (leftHight+1) : (rightHight+1));
    }
    //检验值为val的元素是否存在
    public TreeNode find(TreeNode root,int val){
        if (root == null){
            return null;
        }
        if (root.val == val){
            return root;
        }
        TreeNode leftTree = find(root.left,val);
        if (leftTree != null){
            return leftTree;
        }
        TreeNode rightTree = find(root.right,val);
        if (rightTree != null){
            return rightTree;
        }
        return null;
    }
    //
}
